<?php
namespace app\admin\controller;
use app\admin\model\Subject as subjectModel;
use app\admin\model\Question as modelQuestion;
use app\admin\model\Exam as examModel;
use app\admin\model\Testing as testingModel;
use app\admin\model\TestingLog as logModel;
use think\Db;
use page\Fpage;
use excel\Excel;
class Testinglog extends Common
{
    //答题记录表
    function testingLogList(){
        $logModel = new logModel();
        $testingModel = new testingModel();
        $subjectModel = new subjectModel();
        $examModel = new examModel();
        $testingAllList = $testingModel->testingAllList();
        $subjectAllList = $subjectModel->subjectList();
        $examAllList = $examModel->examAllList();

        $condition = $logModel->condition(input('get.'));
//        dump($condition);exit;
        $testingLogData = $logModel->testingLogData(15,$condition);

//        dump($testingLogData);exit;
        $this->assign('testingAllList',$testingAllList);
        $this->assign('subjectAllList',$subjectAllList);
        $this->assign('examAllList',$examAllList);
        $this->assign('getData',input('get.'));
        $this->assign('list',$testingLogData['list']);
        $this->assign('page',$testingLogData['page']);
        $this->assign('count',$testingLogData['count']);
        return $this->fetch('testingLogList');
    }
    //查看试卷
    function logLook(){
        $logModel = new logModel();
        $id = input('id');
        //答题记录
        $testingLog = $logModel->getTestingLog($id);
        //学员
        $user = Db::name('user')->find($testingLog['userid']);
        //试卷内容
        $exam = examModel::getExam($testingLog['examid']);
        $contentList = $rightList = $wrongList = examModel::putSetting($exam['exam_setting']);
        if($testingLog['answer'] <> ''){
            foreach($testingLog['answer'] as $k=>$v){
                $answer[$v['id']] = $v;
            }
        }
        foreach($contentList as $k=>$v){
            $contentList[$k]['question_analyse'] = $rightList[$k]['question_analyse'] = $wrongList[$k]['question_analyse'] = strip_tags($v['question_analyse']);
            $contentList[$k]['daan'] = $rightList[$k]['daan'] = $wrongList[$k]['daan'] = $answer[$v['id']]['daan'];
            // 如果答案正确，在错题列里取走该题目。如果不正确，在对题列取走该题目
            if($answer[$v['id']]['is_right']==1){
                unset($wrongList[$k]);
            }else{
                unset($rightList[$k]);
            }
        }
//        dump($contentList);exit;
        $this->assign('exam',$exam);
        $this->assign('testingLog',$testingLog);
        $this->assign('contentList',$contentList);
        $this->assign('wrongList',$wrongList);
        $this->assign('rightList',$rightList);
        $this->assign('user',$user);

        return $this->fetch('logLook');
    }
    //批改
    function logCorrect(){
        $logModel = new logModel();
        if(request()->isAjax()){
            $postData = input('post.');
            $logid = $postData['id'];
            $testingLog = $logModel->getTestingLog($logid);
            //如果是没有交卷，记得分0.
            if($testingLog['answer'] == ''){
                $data['score'] = 0;
                $data['status'] = 1;
                $res = Db::name('testing_log')->where('id',$logid)->update($data);
                if($res !== false){
                    exitjson(1,'批阅成功');
                }else{
                    exitjson(0,'批阅失败');
                }
            }else{
                foreach($postData as $k=>$v){
                    if(strpos($k,'is_right_') !== false){
                        $qid = substr($k,9);
                        $is_right_ids[] = $qid;
                        $is_right_arr[$qid] = $v;
                    }
                }
                //主观题批改;
                if($is_right_ids){
                    foreach($testingLog['answer'] as $k=>$v){
                        $answer[$k] = $v;
                        if(in_array($v['id'],$is_right_ids)){
                            $answer[$k]['is_right'] = $is_right_arr[$v['id']];
                        }
                    }
                }
                if($answer){
                    $data['answer'] = json_encode($answer);
                    Db::name('testing_log')->where('id',$logid)->update($data); //更新答题
                }
                $res = $logModel->countScore($logid); //计算试卷分数
                if($res){
                    $dd['status'] = 1;
                    $res = Db::name('testing_log')->where('id',$logid)->update($dd);
                    if($res !== false){
                        exitjson(1,'批阅成功');
                    }else{
                        exitjson(0,'批阅失败');
                    }
                }else{
                    exitjson(0,'计算分数失败');
                }

            }

        }else{
            $id = input('id');
            //答题记录
            $testingLog = $logModel->getTestingLog($id);
            //学员
            $user = Db::name('user')->find($testingLog['userid']);
            //试卷内容
            $exam = examModel::getExam($testingLog['examid']);
            $contentList = $rightList = $wrongList = examModel::putSetting($exam['exam_setting']);
            if($testingLog['answer'] <> ''){
                foreach($testingLog['answer'] as $k=>$v){
                    $answer[$v['id']] = $v;
                }
            }
            foreach($contentList as $k=>$v){
                $contentList[$k]['question_analyse'] = $rightList[$k]['question_analyse'] = $wrongList[$k]['question_analyse'] = strip_tags($v['question_analyse']);
                $contentList[$k]['daan'] = $rightList[$k]['daan'] = $wrongList[$k]['daan'] = $answer[$v['id']]['daan'];
                // 如果答案正确，在错题列里取走该题目。如果不正确，在对题列取走该题目
                if($answer[$v['id']]['is_right']==1){
                    unset($wrongList[$k]);
                }else{
                    unset($rightList[$k]);
                }
            }
//                    dump($contentList);exit;
            $this->assign('exam',$exam);
            $this->assign('testingLog',$testingLog);
            $this->assign('contentList',$contentList);
            $this->assign('wrongList',$wrongList);
            $this->assign('rightList',$rightList);
            $this->assign('user',$user);

            return $this->fetch('logCorrect');
        }
    }

    function logDel(){
        $id = input('id');
        if($id>0){
            $res = Db::name('testing_log')->delete($id);
            if($res){
                exitjson(1,'删除成功');
            }else{
                exitjson(0,'删除失败');
            }
        }
    }

    //文科成绩统计
    function countList(){
        $logModel = new logModel();
        $testingModel = new testingModel();
        $getData = input('get.');
        $art_type = 1;
        //入学检测列表
        $testingAllList = $testingModel->testingAllList();
        //某期入学检测
        if($getData['testingid']>0){
            $testingid = $getData['testingid'];
            $testing = Db::name('testing')->find($testingid);
            if(!$testing){
                exitjson(0,'参数错误testingid');
            }
        }else{
            $testing = Db::name('testing')->where('is_active',1)->find();
            $testingid = $testing['testingid'];
        }
        //分页数据
        $p = $getData['page']>0?$getData['page']:1;
        $res = $logModel->testingRank($testingid,$art_type,$p);
        $page = new \page\Fpage($p,$res['count'][0]['logCount'],20,'testingid='.$testing['testingid'].'&');
        $pageList = $page->pagelist();

        $this->assign('list',$res['list']);
        $this->assign('pageList',$pageList);
        $this->assign('count',$res['count'][0]['logCount']);
        $this->assign('testingAllList',$testingAllList);
        $this->assign('getData',$getData);
        $this->assign('testing',$testing);

        return $this->fetch('countList');
    }

    //理科成绩统计
    function likeList(){
        $logModel = new logModel();
        $testingModel = new testingModel();
        $getData = input('get.');
        $art_type = 2;
        //入学检测列表
        $testingAllList = $testingModel->testingAllList();
        //某期入学检测
        if($getData['testingid']>0){
            $testingid = $getData['testingid'];
            $testing = Db::name('testing')->find($testingid);
            if(!$testing){
                exitjson(0,'参数错误testingid');
            }
        }else{
            $testing = Db::name('testing')->where('is_active',1)->find();
            $testingid = $testing['testingid'];
        }
        //分页数据
        $p = $getData['page']>0?$getData['page']:1;
        $res = $logModel->testingRank($testingid,$art_type,$p);
        $page = new \page\Fpage($p,$res['count'][0]['logCount'],20,'testingid='.$testing['testingid'].'&');
        $pageList = $page->pagelist();

        $this->assign('list',$res['list']);
        $this->assign('pageList',$pageList);
        $this->assign('count',$res['count'][0]['logCount']);
        $this->assign('testingAllList',$testingAllList);
        $this->assign('getData',$getData);
        $this->assign('testing',$testing);

        return $this->fetch('likeList');
    }

    //成绩导出
    function logDaochu(){
        $logModel = new logModel();
        $getData = input('route.');
        $xlsName = "答题记录表";
        $xlsCell  = array(
            array('user_name','学号'),
            array('testing_name','入学检测'),
            array('subject_name','科目'),
            array('exam_name','试卷名称'),
            array('score','分数'),
            array('add_time','开始时间'),
            array('use_time','用时'),
            array('status','已批阅'),
        );
        $list = $logModel->testingLogAllList($getData);
        foreach($list as $k=>$v){
            $xlsData[$k]['user_name'] = $v['user_name'];
            $xlsData[$k]['testing_name'] = $v['testing_name'];
            $xlsData[$k]['subject_name'] = $v['subject_name'];
            $xlsData[$k]['exam_name'] = $v['exam_name'];
            $xlsData[$k]['score'] = $v['score'];
            $xlsData[$k]['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            $xlsData[$k]['use_time'] = gmdate('H:i:s',$v['use_time']);
            $xlsData[$k]['status'] = $v['status']==1?'是':'否';
        }

        $excelModel = new \excel\Excel();

        $excelModel->exportExcel($xlsName,$xlsCell,$xlsData);
    }

    //导出分数统计
    public function daochu(){
        $logModel = new logModel();
        $getData = input('route.');
        $testingid = $getData['testingid'];
//        dump($getData);exit;
        if($testingid>0){
            $testing = Db::name('testing')->find($testingid);
            $art_type = $getData['art_type']>0?$getData['art_type']:1;
            if($art_type==2){
                $xlsName = "理科".$testing['testing_name']."成绩统计";
                    $xlsCell  = array(
                    array('user_name','学号'),
                    array('yuwen','语文'),
                    array('shuxue','数学'),
                    array('yingyu','英语'),
                    array('wuli','物理'),
                    array('huaxue','化学'),
                    array('total_score','总分'),
                );
            }else{
                $xlsName = "文科".$testing['testing_name']."成绩统计";
                    $xlsCell  = array(
                        array('user_name','学号'),
                        array('yuwen','语文'),
                        array('shuxue','数学'),
                        array('yingyu','英语'),
                        array('dili','地理'),
                        array('lishi','历史'),
                        array('total_score','总分'),
                    );
            }
            
            $xlsData = $logModel->testingRankAllList($testingid,$art_type);
            // dump($xlsData);exit;
            $excelModel = new \excel\Excel();

            $excelModel->exportExcel($xlsName,$xlsCell,$xlsData);
        }

    }


}